import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuanqi.CityFriends;
import com.yuanqi.Mapper.CommentMapper;
import com.yuanqi.Common.Pojo.Vo.FollowVo;
import com.yuanqi.Common.Pojo.Vo.commentVo;
import com.yuanqi.Common.Pojo.entity.Comment;
import com.yuanqi.Utils.AliCaptcha;
import org.junit.jupiter.api.Test;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

//@SpringBootTest(classes = CityFriends.class)
public class test {

    @Resource
    AliCaptcha aliCaptcha;

    @Test
    void send() throws Exception {
//        aliCaptcha.send("18180452398","S32Z");
        String hashpw = BCrypt.hashpw("123456", BCrypt.gensalt());
        System.out.println(hashpw);
        System.out.println(BCrypt.checkpw("123456", hashpw));
    }

    @Test
    void time() throws ParseException {
        Date date = new Date();
        System.out.println(date.getTime());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String format = simpleDateFormat.format(date);
        String[] split = format.split(" ");
        format = split[0] + " " + "00:00:00";
        Date parse = simpleDateFormat.parse(format);
        System.out.println(parse.getTime());
    }

    @Resource
    CommentMapper commentMapper;

    @Test
    void getComment() {
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Comment::getPostId, 1);
        List<Comment> comments = commentMapper.selectList(wrapper);
        List<Tree<Integer>> treeList = TreeUtil.build(comments, 0, (name, tree) -> {
            tree.setParentId(name.getFid());
            tree.setId(name.getId());
            tree.putAll(BeanUtil.beanToMap(name));

        });
        String jsonStr = JSONUtil.toJsonStr(treeList);
        JSONArray jsonArray = JSONUtil.parseArray(jsonStr);
        List<Comment> list = new ArrayList<>();
        // 遍历 JSONArray 并转换为 Comment 对象
        for (int i = 0; i < jsonArray.size(); i++) {
            Comment comment = jsonArray.getBean(i, Comment.class);
            list.add(comment);
        }
        System.out.println(list);
        list.sort(( t1, t2)-> {
            return  t2.getFnum().compareTo(t1.getFnum());
        });
        List<commentVo> list1 = new ArrayList<>();
        getlist(list,list1);
        System.out.println(list1);
        System.out.println(list);
    }
    private void getlist(List<Comment> list, List<commentVo> list1) {
        for (Comment comment : list) {
            commentVo bean = BeanUtil.toBean(comment, commentVo.class);
            list1.add(bean);
            if (comment.getChildren()!=null)
            {
                getlist(comment.getChildren(),list1);
            }

        }
    }
    @Test
    void test1()
    {
        FollowVo followVo = new FollowVo();
        followVo.setAge(12);
        FollowVo followVo1 = new FollowVo();
        followVo1.setAge(12);
        List<FollowVo> list = new ArrayList<>();
        list.add(followVo);
        list.add(followVo1);
        Iterator<FollowVo> iterator = list.iterator();
        while (iterator.hasNext()) {
            if (iterator.next().getAge()==12)
            {
                iterator.remove();
            }
        }

        System.out.println(list);
    }
}
